<?php

namespace TpAmis\Traits;

use think\facade\Filesystem;

trait UploadTrait
{
    /**
     * 图片上传路径
     *
     * @return string
     */
    public function uploadImagePath()
    {
        return admin_url('upload_image');
    }

    public function uploadImage()
    {
        return $this->upload('image');
    }

    /**
     * 文件上传路径
     *
     * @return string
     */
    public function uploadFilePath()
    {
        return admin_url('upload_file');
    }

    public function uploadFile()
    {
        return $this->upload();
    }

    /**
     * 富文本编辑器上传路径
     *
     * @return string
     */
    public function uploadRichPath($needPrefix = false)
    {
        return admin_url('upload_rich', $needPrefix);
    }

    public function uploadRich()
    {
        $fromWangEditor = false;
        $file = request()->file('file');

        if (!$file) {
            $fromWangEditor = true;
            $file = request()->file('wangeditor-uploaded-image');
            if (!$file) {
                $file = request()->file('wangeditor-uploaded-video');
            }
        }

        if (!$file) {
            return $this->response()->additional(['errno' => 1])->fail(lang('admin.upload_file_error'));
        }
        $link = Filesystem::putFile(config('admin.upload.directory.rich'), $file, 'uniqid');

        if ($fromWangEditor) {
            return $this->response()->additional(['errno' => 0])->success(['url' => $link]);
        }

        return $this->response()->additional(compact('link'))->success(compact('link'));
    }

    protected function upload($type = 'file')
    {
        $file = request()->file('file');

        if (!$file) {
            return $this->response()->fail(lang('admin.upload_file_error'));
        }
        $path = Filesystem::putFile(config('admin.upload.directory.rich'), $file, 'uniqid');

        return $this->response()->success(['value' => $path]);
    }

    //分片上传
    public function chunkUploadStart()
    {
        // $uploadId = (string) Str::uuid();

        // cache()->put($uploadId, [], 600);

        // $path = Filesystem::putFile(config('admin.upload.directory.rich'), $file, 'uniqid');

        // app('filesystem')->makeDirectory(storage_path('app/public/chunk/' . $uploadId));

        // return $this->response()->success(compact('uploadId'));
    }
    //分片上传
    public function chunkUpload()
    {
        // $uploadId = request('uploadId');
        // $partNumber = request('partNumber');
        // $file = request()->file('file');

        // $path = 'chunk/' . $uploadId;

        // $file->storeAs($path, $partNumber, 'public');

        // $eTag = md5(Storage::disk('public')->get($path . '/' . $partNumber));

        // return $this->response()->success(compact('eTag'));
    }
    //分片上传
    public function chunkUploadFinish()
    {
        // $fileName = request('filename');
        // $partList = request('partList');
        // $uploadId = request('uploadId');
        // $type = request('t', 'uploads');

        // $ext = pathinfo($fileName, PATHINFO_EXTENSION);
        // $path = $type . '/' . $uploadId . '.' . $ext;
        // $fullPath = storage_path('app/public/' . $path);

        // make_dir(dirname($fullPath));

        // for ($i = 0; $i < count($partList); $i++) {
        //     $partNumber = $partList[$i]['partNumber'];
        //     $eTag = $partList[$i]['eTag'];

        //     $partPath = 'chunk/' . $uploadId . '/' . $partNumber;

        //     $partETag = md5(Storage::disk('public')->get($partPath));

        //     if ($eTag != $partETag) {
        //         return $this->response()->fail('分片上传失败');
        //     }

        //     file_put_contents($fullPath, Storage::disk('public')->get($partPath), FILE_APPEND);
        // }

        // clearstatcache();

        // app('files')->deleteDirectory(storage_path('app/public/chunk/' . $uploadId));

        // return $this->response()->success(['value' => $path], '上传成功');
    }
}
